load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl"
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_csm.ncl"
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.ncl"
;------------------------------------------------------------------
;read and combine data
;------------------------------------------------------------------
;;;; Mask data
MM=addfile("DATA/FIG_1/TMPA_mask_remap.nc","r")
msk=MM->landseamask1(::-1,:)

X=addfile("DATA/FIG_1/Climatology_1980_2020.nc","r")
event=X->TC_EVENTS
residence=X->TC_RESIDENCE
inten=X->TC_INT_EVENT
contr=X->TC_CONTRIBUTION
contr1=contr*msk
contr1 :=where(contr1.le.0.0,contr1@_FillValue,contr1)
copy_VarMeta(contr,contr1)

;----------------------------------------------------------
; Plot map
;----------------------------------------------------------


plot = new((/2/),"graphic")
type = (/"eps","png"/)

files = "FIG_MAIN/Fig_1"

do m = 0, 1

  wks = gsn_open_wks(type(m),files)
   ;gsn_define_colormap(wks,"BlAqGrYeOrReVi200")
   gsn_define_colormap(wks,"precip3_16lev")   ;    "precip3_16lev"
;  gsn_reverse_colormap(wks)
;"GSFC_landsat_udf_density"
  res = True
  res@gsnDraw = False
  res@gsnFrame = False
  res@gsnMaximize = False
  res@vpXF = 0.05               ; default is 0.2 (aspect ratio)
  res@vpYF = 0.96              ; default is 0.8
  res@vpHeightF = 0.29             ; default is 0.6
  res@vpWidthF = 0.8

  res@mpMinLatF           = -60 ;-89.75         ; map area
  res@mpMaxLatF           = 85  ;89.75        ; latitudes
  res@mpMinLonF           = -179.75       ; and
  res@mpMaxLonF           = 179.75       ; longitudes

  res@mpPerimOn = False
  res@mpPerimLineThicknessF = 3.0
  res@mpDataBaseVersion     = "MediumRes"
  res@mpNationalLineColor = "gray20"
  res@mpGeophysicalLineColor = "gray20"
  res@mpOutlineOn = True
  res@mpOutlineBoundarySets = "National"
  
  ;------------JK  Robinson Projection
  res@mpProjection = "Robinson"
  res@mpGridAndLimbOn = True
  res@mpGridLatSpacingF = 30.
  res@mpGridLonSpacingF = 60.
  res@gsnRightString = ""
;------------JK
  
  
 
  res@cnFillOn = True                    ; no map fill
  res@cnFillMode = "Meshfill"
  res@cnFillOpacityF = 1.0

  res@cnLinesOn = False
  res@cnLineLabelsOn = False
  res@gsnAddCyclic = False
  res@tmXBLabelFontHeightF = 0.008
  res@tmYLLabelFontHeightF = 0.008
  
  res@lbTitleFontHeightF = 0.013
  res@lbLabelFontHeightF = 0.011
  res@tiMainFontHeightF = 0.016
  res@gsnLeftStringFontHeightF = 0.02
  res@mpFillOn = False
  
  ;;label

  res@lbAutoManage       = False          ; Necessary to control sizes
  
     res@lbLabelBarOn               = True

    ; Set resources for when we turn on labelbar later
    res@lbTitleFontHeightF         = .010                 ; make title smaller
    res@lbLabelAutoStride          =True
    res@lbLabelFontHeightF         = 0.01   ;0.011
    res@pmLabelBarHeightF          = 0.2  ;0.05
    res@lbBoxEndCapStyle           = "TriangleHighEnd"
    res@lbOrientation              = "Vertical"     ;"Horizontal"        ; orientation
    res@lbLabelStride              = 2
    res@lbTitleAngleF = 270.0
    res@lbTitleDirection = "Across"
    res@lbTitlePosition = "right"
    res@pmLabelBarWidthF      = 0.05              ; default is shorter
    res@pmLabelBarHeightF     = 0.24              ; default is taller
    res@lbPerimOn             = False             ; default has box
    
  res2=res
  
  res2@tmXBLabelsOn         = False              ; do not draw bottom labels
  res2@lbTitleString = "[events]"
  res2@gsnLeftString = "A) Total number of TC landfalls (1980-2020)"
  res2@gsnLeftStringFontHeightF = 0.016

  ;res2@pmLabelBarOrthogonalPosF = 0.05  ; JK

  clevs := fspan(0,100,11)  ; JK: Please change the min-max range for TC Event count
  res2@gsnSpreadColors = True
  res2@cnLevelSelectionMode= "ExplicitLevels"
  res2@cnLevels := clevs
  plot(0) = gsn_csm_contour_map(wks, event, res2) ; JK: Please change the data for TC Event Counts
 
  
    
  clevs := fspan(0,10,11)
  res2@vpXF = 0.05               ; default is 0.2 (aspect ratio)
  res2@vpYF = 0.62              ; default is 0.8
  res2@lbTitleString = "[%]"
  res2@gsnLeftString = "B) TC-related precipitation contribution"
  res2@cnLevels := clevs
  res4=res2
  res4@tmXBLabelsOn         = True  
  res4@tmYLLabelsOn         = True 
  
  plot(1) = gsn_csm_contour_map(wks, contr1, res4)
  

  ;clevs := fspan(0,40,11)
  ;res2@vpXF = 0.05               ; default is 0.2 (aspect ratio)
  ;res2@vpYF = 0.30              ; default is 0.8
  ;res2@lbTitleString = "[mm/event]"
  ;res2@gsnLeftString = "C) TC-related precipitation Intensity"
  ;res2@cnLevels := clevs
  ;res5=res2
  ;res5@tmXBLabelsOn         = True  
  ;res5@tmYRLabelsOn         = True 
  ;res5@tmYLLabelsOn         = False 
  ;res5@lbLabelStride              = 3
  ;res5@pmLabelBarOrthogonalPosF = 0.15  ; JK

  ;plot(2) = gsn_csm_contour_map(wks, inten, res5)

  pgres = True
  pgres@gsEdgesOn = True
  pgres@gsFillColor = -1
  pgres@gsEdgeColor = 1
  pgres@gsEdgeDashPattern = 0
  pgres@gsEdgeThicknessF = 3.0
  dum1 = gsn_add_polygon(wks,plot(0),(/-130,-50,-50,-130/),(/5, 5, 50, 50/),pgres)
  dum2 = gsn_add_polygon(wks,plot(0),(/100,160,160,100/),(/5, 5, 50, 50/),pgres)
  dum3 = gsn_add_polygon(wks,plot(0),(/40,100,100,40/),(/5, 5, 50, 50/),pgres)
  dum4 = gsn_add_polygon(wks,plot(0),(/100,179,179,100/),(/-5, -5, -50, -50/),pgres)
  dum5 = gsn_add_polygon(wks,plot(0),(/25,65,65,25/),(/-5, -5, -50, -50/),pgres)

  txres = True
  txres@txJust = "CenterLeft"
  txres@txFontColor = "darkviolet"
  txres@txAngleF = 0.0
  txres@txFontThicknessF = 4.0
  txres@txFontHeightF = 0.008

  regs = (/"NCA", "EAS", "SWA", "OCE", "EAF"/)

  tdum1 = gsn_add_text(wks,plot(0),regs(0),-128,10,txres)
  tdum2 = gsn_add_text(wks,plot(0),regs(1),139,10,txres)
  tdum3 = gsn_add_text(wks,plot(0),regs(2),75,45,txres)
  tdum4 = gsn_add_text(wks,plot(0),regs(3),102,-44,txres)
  tdum5 = gsn_add_text(wks,plot(0),regs(4),44,-44,txres)


    
    
  draw(plot)
  frame(wks)

end do

system("convert -density 300 "+ files+".eps " +files+".png")